﻿var ns = namespace("kaskelot.sp.ui");

ns.byggnadhandelselogg = function () {

	function getList() {
		var id = getParameterByName('ByggnadId');

		$(".blockable-container").block({ message: null });

		$.ajax({
			url: L_Menu_BaseUrl + "/_vti_bin/Kaskelot.SP.Resources/ClientByggnadService.svc/GetByggnadHandelseLoggByByggnadId",
			type: "GET",
			data: { id: id },
			processData: true,
			cache: false,
			success:
                function (data) {
                	if (!data.GetByggnadHandelseLoggByByggnadIdResult.HasError) {
                		$("#handelselogg-container tbody").html(
							$("#handelselogg-template").render(data.GetByggnadHandelseLoggByByggnadIdResult.Data)
						);
                	}
                	$(".blockable-container").unblock();
                },
			error:
                function (xhr) {
                	$(".blockable-container").unblock();
                	kaskelot.sp.ui.prompts.showErrorPrompt(xhr.responseText);
                }
		});
	}

	function showEditForm(id) {
		var byggnadId = getParameterByName('ByggnadId');
		var loggId = id;
		if (loggId === undefined)
			loggId = "";

		var selector = "#byggnadhandelselogg-form";
		kaskelot.sp.ui.formutils.getAndShowForm({
			params: {
				formName: "ByggnadHandelseLoggForm",
				idString: loggId,
				parentIdString: byggnadId
			},
			selector: selector,
			openFunction: function () {
				$("#Datum").datepicker();
			},
			okFunction: function () {
				$(selector).parent().block();

				var prog = kaskelot.sp.ui.formutils.getObject(selector);
				var data = "{\"form\":" + JSON.stringify(prog) + "}";

				var method = "";
				if (loggId === "")
					method = "CreateByggnadHandelseLogg";
				else
					method = "UpdateByggnadHandelseLogg";

				$.ajax({
					url: L_Menu_BaseUrl + "/_vti_bin/Kaskelot.SP.Resources/ClientByggnadService.svc/" + method,
					data: data,
					type: "POST",
					processData: false,
					contentType: "application/json",
					dataType: "json",
					success: function (resp) {
						if (resp[method + "Result"].HasError) {
							kaskelot.sp.ui.formutils.showError(selector, resp[method + "Result"].ErrorMessage);
						}
						else {
							getList();
							$(selector).dialog("close");
						}
					},
					error: function (xhr) {
						kaskelot.sp.ui.formutils.showError(selector, xhr.responseText);
					}
				});
			}
		});
	}

	function showDeleteForm(id) {
		var selector = "#byggnadhandelselogg-form";
		kaskelot.sp.ui.formutils.getAndShowForm({
			params: {
				formName: "ByggnadHandelseLoggForm",
				idString: id,
				parentIdString: ""
			},
			selector: selector,
			openFunction: function () {
				$("#byggnadhandelselogg-form").dialog({ title: "Ta bort händelseloggpost" });

				$("#byggnadhandelselogg-form #Text").attr("disabled", "disabled");
				$("#byggnadhandelselogg-form #Datum").attr("disabled", "disabled");
				$("#byggnadhandelselogg-form #AtgardKravs").attr("disabled", "disabled");

				$(".chzn-select-deselect").chosen({ allow_single_deselect: true, disable_search: true });
			},
			okFunction: function () {
				$(selector).parent().block();

				var prog = kaskelot.sp.ui.formutils.getObject(selector);
				var data = "{\"form\":" + JSON.stringify(prog) + "}";

				var method = "DeleteByggnadHandelseLogg";

				$.ajax({
					url: L_Menu_BaseUrl + "/_vti_bin/Kaskelot.SP.Resources/ClientByggnadService.svc/" + method,
					data: data,
					type: "POST",
					processData: false,
					contentType: "application/json",
					dataType: "json",
					success: function (resp) {
						if (resp[method + "Result"].HasError) {
							kaskelot.sp.ui.formutils.showError(selector, resp[method + "Result"].ErrorMessage);
						}
						else {
							$(selector).dialog("close");
							getList();
						}
					},
					error: function (xhr) {
						kaskelot.sp.ui.formutils.showError(selector, xhr.responseText);
					}
				});
			}
		});
	}

	return {
		getList: getList,
		showEditForm: showEditForm,
		showDeleteForm: showDeleteForm
	}
}();